<?php

class Model_Locations {
	
	protected $location_id;
	protected $name;
	protected $parent_id;
	protected $slug;
	protected $enabled;
	
	public function __construct(array $options = null) {
		if (is_array ( $options )) {
			$this->setOptions ( $options );
		}
	}
	
	public function getURI() {
		if (Params::getParam ( 'category' )) {
			$category = Params::getParam ( 'category' );
			$uri = site_url () . '/' . $this->getSlug () . '/' . $category->getSectionURI () . '/';
		} else {
			$uri = site_url () . '/' . $this->getSlug () . '/';
		}
		return sanitizeURI ( $uri );
	}
	
	public function toRoot() {
		return Model_Mapper_Locations::newInstance ()->toRoot ( $this );
	}
	
	public function toTree() {
		return Model_Mapper_Locations::newInstance ()->toTree ( $this );
	}
	
	public function tree() {
		return Model_Mapper_Locations::newInstance ()->tree ();
	}
	public function hasChildrens() {
		return Model_Mapper_Locations::newInstance ()->hasChildrens ( $this );
	}
	public function getChildrens($recursive = false) {
		$childrens = Model_Mapper_Locations::newInstance ()->getChildrens ( $this, $recursive );
		if ($recursive) {
			return $childrens;
		} else {
			return Model_Mapper_Locations::newInstance ()->_extend ( $childrens );
		}
	}
	
	public function getItems() {
		return Model_Mapper_Locations::newInstance ()->getItems ( $this );
	}
	
	public function getItemCount() {
		return Model_Mapper_Locations::newInstance ()->getItemCount ( $this );
	}
	
	public function __set($name, $value) {
		$method = 'set' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		$this->$method ( $value );
	}
	
	public function __get($name) {
		$method = 'get' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		return $this->$method ();
	}
	
	public function setOptions(array $options) {
		$methods = get_class_methods ( $this );
		foreach ( $options as $key => $value ) {
			$method = 'set' . ucfirst ( $key );
			if (in_array ( $method, $methods )) {
				$this->$method ( $value );
			}
		}
	}
	
	public function setLocation_id($value) {
		$this->location_id = $value;
	}
	
	public function getLocation_id() {
		return $this->location_id;
	}
	
	public function setName($value) {
		$this->name = $value;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function setParent_id($value) {
		$this->parent_id = $value;
	}
	
	public function getParent_id() {
		return $this->parent_id;
	}
	
	public function setSlug($value) {
		$this->slug = $value;
	}
	
	public function getSlug() {
		return $this->slug;
	}
	
	public function setEnabled($value) {
		$this->enabled = $value;
	}
	
	public function getEnabled() {
		return $this->enabled;
	}
	
	public function toArray() {
		return get_object_vars ( $this );
	}

}

